package pers.cz.business;

import pers.cz.mapper.NotifyMessageMapper;
import pers.cz.model.NotifyMessage;
import pers.cz.mybatisPlugin.PageRowBounds;
import pers.cz.pub.AbstractBusiness;
import pers.cz.pub.MyThreadLocals;

import java.util.Date;
import java.util.List;

/**
 * @program: PostGirl-panent
 * @description: MessageBusiness
 * @author: Cheng Zhi
 * @create: 2021-10-08 09:24
 **/
public class MessageBusiness extends AbstractBusiness {

    /**
     * 查询该用户下所有消息
     * @param userId
     * @return
     */
    public List<NotifyMessage> queryAllNotifyMessage(Integer userId, Integer pageStart, Integer pageLimit) {

        PageRowBounds pageRowBounds = new PageRowBounds(pageStart, pageLimit);
        return this.getMapper().getBean(NotifyMessageMapper.class).queryAllNotifyMessage(pageRowBounds,userId);
    }

    /**
     * 查询该用户下所有已读或者未读消息
     * @param userId
     * @param type
     * @return
     */
    public List<NotifyMessage> queryAllNotifyMessageByType(Integer userId, String type, Integer pageStart, Integer pageLimit) {

        PageRowBounds pageRowBounds = new PageRowBounds(pageStart, pageLimit);
        return this.getMapper().getBean(NotifyMessageMapper.class).queryAllNotifyMessageByType(pageRowBounds, userId, type);
    }

    /**
     * 根据消息类型查询该用户下所有消息
     * @param userId
     * @param messageType
     * @return
     */
    public List<NotifyMessage> queryAllNotifyMessageByMessageType(Integer userId, String messageType, Integer pageStart, Integer pageLimit) {
        PageRowBounds pageRowBounds = new PageRowBounds(pageStart, pageLimit);
        return this.getMapper().getBean(NotifyMessageMapper.class).queryAllNotifyMessageByMessageType(pageRowBounds, userId, messageType);
    }

    /**
     * 修改消息为已读或者未读
     * @param userId
     * @param id
     * @param type
     * @return
     */
    public Integer updateNotifyMessageType(Integer userId, Integer id, String type) {
        NotifyMessage notifyMessage = new NotifyMessage();
        notifyMessage.setId(id);
        notifyMessage.setType(type);
        int count = this.getMapper(NotifyMessageMapper.class).updateByPrimaryKeySelective(notifyMessage);
        return count;
    }

    /**
     * 修改消息类型
     * @param userId
     * @param id
     * @param messageType
     * @return
     */
    public Integer updateNotifyMessageMessageType(Integer userId, Integer id, String messageType) {

        NotifyMessage notifyMessage = new NotifyMessage();
        notifyMessage.setId(id);
        notifyMessage.setMessageType(messageType);
        int count = this.getMapper(NotifyMessageMapper.class).updateByPrimaryKeySelective(notifyMessage);
        return count;
    }

    /**
     * 新增消息
     * @return
     */
    public Integer insertNotifyMessage(NotifyMessage message) {
        int count = this.getMapper().getBean(NotifyMessageMapper.class).insert(message);
        return count;
    }

    /**
     * 根据主键删除消息
     * @param id
     * @return
     */
    public Integer deleteMessageById(Integer id) {

        return this.getMapper().getBean(NotifyMessageMapper.class).deleteByPrimaryKey(id);
    }

    /**
     * 根据条件查询通知消息
     * @param userId
     * @param messageType
     * @param pageStart
     * @param pageLimit
     * @param type
     * @param startDate
     * @param endDate
     * @param theme
     * @return
     */
    public List<NotifyMessage> queryNotifyMessagetByCondition(Integer userId, String messageType, Integer pageStart, Integer pageLimit, String type, Date startDate, Date endDate, String theme) {

        PageRowBounds pageRowBounds = new PageRowBounds(pageStart, pageLimit);
        return this.getMapper().getBean(NotifyMessageMapper.class).selectByCodition(userId, messageType, type, startDate, endDate, theme);
    }

    /**
     * 获取用户未读消息总量
     * @param userId
     * @return
     */
    public Integer quertNotifyMessageCount(int userId, String type) {

        System.out.println(MyThreadLocals.myThreadInterface.get());
        MyThreadLocals.myThreadInterface.remove();
        System.out.println(MyThreadLocals.myThreadInterface.get());

        for (int i=0; i< 10; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {

                }
            });
            thread.start();
        }
        if (type == null || "".equals(type)) {
            type = "未读";
        }
        Integer count = this.getMapper().getBean(NotifyMessageMapper.class).quertNotifyMessageCount(userId, type);
        return count;
    }

    /**
     * 修改全部消息类型
     * @param userId
     * @param type
     * @return
     */
    public Integer updateNotifyMessageMessageTypeAll(Integer userId, String type) {

        this.getMapper(NotifyMessageMapper.class).updateMessageTypeAllByUserId(userId, type);
        return 1;
    }
}
